package com.hrabun.goodmorningalarm;

import java.util.Calendar;
import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;

public class AlarmItem implements Parcelable {
	Database db;

	String id;
	String alarmName;
	Date alarmTime;
	boolean monday;
	boolean tuesday;
	boolean wednesday;
	boolean thursday;
	boolean friday;
	boolean saturday;
	boolean sunday;
	boolean active;
	String radioAssignedID;

	public AlarmItem(String aName, Date aTime, boolean mon, boolean tue,
			boolean wed, boolean thur, boolean fri, boolean sat, boolean sun,
			boolean _active) {
		this.alarmName = aName;
		this.alarmTime = aTime;
		this.monday = mon;
		this.tuesday = tue;
		this.wednesday = wed;
		this.thursday = thur;
		this.friday = fri;
		this.saturday = sat;
		this.sunday = sun;
		this.active = _active;
		this.radioAssignedID = "";
	}

	public AlarmItem() {
		// TODO Auto-generated constructor stub
	}

	private AlarmItem(Parcel parcel) {
		
	}
	
	public void activate() {
		active = true;
		db = new Database(MainActivity.getAppContext());
		db.changeActiveState(String.valueOf(this.id), true);

		createAlarm();
	}

	public void deactivate() {
		active = false;
		db = new Database(MainActivity.getAppContext());
		db.changeActiveState(String.valueOf(this.id), false);

		cancelAlarm();
	}

	public void rename(String newName) {
		alarmName = newName;
	}

	public AlarmItem getAlarmItemByTime(Date aTime) {
		db = new Database(MainActivity.getAppContext());
		return db.getAlarmItemByTime(aTime);
	}

	public AlarmItem getAlarmItemById(String id) {
		db = new Database(MainActivity.getAppContext());
		return db.getAlarmItemById(id);
	}

	public void alarmAdd() {
		db = new Database(MainActivity.getAppContext());
		this.id = db.saveAlarmItem(this);
		createAlarm();
	}

	public void setId(String _id) {
		id = _id;
	}

	private void createAlarm() {
		Alarm alarm = new Alarm();
		alarm.setAlarm(MainActivity.getAppContext(), this.id, this.alarmTime);
	}

	private void cancelAlarm() {
		Alarm alarm = new Alarm();
		alarm.cancelAlarm(MainActivity.getAppContext(), this.id);
	}

	public void setRadioAssigned(String id) {
		this.radioAssignedID = id;
	}

	public String getRadioAssignedID() {
		return radioAssignedID;
	}
		
	public boolean isValidForToday(int day) {
		switch (day) {
		case 1:	return this.sunday;
		case 2: return this.monday;
		case 3: return this.tuesday;
		case 4: return this.wednesday;
		case 5: return this.thursday;
		case 6: return this.friday;
		case 7: return this.saturday;
		default: return false;
		}
	}

	@SuppressWarnings("unused")
	public boolean isValidForTime() {
		boolean result = false;
		Calendar alarmCalendar = Calendar.getInstance();
		alarmCalendar.setTime(this.alarmTime);
		Calendar currenTime = Calendar.getInstance();
		currenTime.set(Calendar.SECOND, 0);
		currenTime.set(Calendar.MILLISECOND, 0);
		int res = currenTime.compareTo(alarmCalendar);
		if (res == 0) result = true;
		return result;
	}
	
	public void deleteAlarmItem() {
		this.active = false;
		deactivate();
		db = new Database(MainActivity.getAppContext());
		db.deleteAlarmItem(this.id);
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeValue(this);
	}
	
	public static final Parcelable.Creator<AlarmItem> CREATOR = new Creator<AlarmItem>() {
		
		@Override
		public AlarmItem[] newArray(int size) {
			// TODO Auto-generated method stub
			return new AlarmItem[size];
		}
		
		@Override
		public AlarmItem createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			return new AlarmItem(source);
		}
	};

	public void setAlarmTime(Date alarmTime) {
		this.alarmTime = alarmTime;
	}
	
	public void setMonday(boolean monday) {
		this.monday = monday;
	}
	
	public void setThursday(boolean thursday) {
		this.thursday = thursday;
	}
	
	public void setTuesday(boolean tuesday) {
		this.tuesday = tuesday;
	}
	
	public void setFriday(boolean friday) {
		this.friday = friday;
	}
	
	public void setSaturday(boolean saturday) {
		this.saturday = saturday;
	}
	
	public void setSunday(boolean sunday) {
		this.sunday = sunday;
	}
	
	public void setWednesday(boolean wednesday) {
		this.wednesday = wednesday;
	}
	
	public void setActive(boolean active) {
		this.active = active;
	}

	public void updateAlarmItem() {
		deactivate();
		db = new Database(MainActivity.getAppContext());
		db.updateAlarmItem(this);
		activate();
	}
}
